#include "threadpool.h"

int userInit(char *path);

//int recvn(int netfd, void* buf, int length);

void* threadFunc(void* arg);


int makeWorker(threadPool_t *pthreadPool){
    for(int i=0;i<pthreadPool->workerNum;i++){
        pthread_create(&pthreadPool->tidArr[i],NULL,threadFunc,pthreadPool);
    }

    return 0;
}
void* threadFunc(void* arg){
    threadPool_t* pthreadPool=(threadPool_t*)arg;
    while(1){
        pthread_mutex_lock(&pthreadPool->taskQueue.mutex);
        while(pthreadPool->taskQueue.queueSize<=0){
            pthread_cond_wait(&pthreadPool->taskQueue.cond,&pthreadPool->taskQueue.mutex);
        }
        int netfd=pthreadPool->taskQueue.head->netfd;
        deQueue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->taskQueue.mutex);

        printf("tid = %lu\n",pthread_self());
        printf("child get a task!\n");

        //业务区
        char real_path[4000] = {0};
        char username[1024] = {0};
        char password[1024] = {0};
        int virtual_start = 0;
        train_t train;
        //收取密码
        int passwd_check;
        int length;
        recv(netfd,&length,sizeof(int),0);
        recv(netfd,username,length,0);
        printf("username: %s\n",username);

        recv(netfd,&length,sizeof(int),0);
        recv(netfd,password,length,0);
        printf("text: %s\n", password);

        //密码验证
        passwd_check = passwdVerify(username,password);
        printf("passwd_check = %d\n",passwd_check);
        if(passwd_check == 1)//密码正确
        {
            char buf[1024] = {0};
            getcwd(buf,sizeof(buf));
            sprintf(real_path,"%s%s%s",buf,"/user/",username);
            userInit(real_path);
            send(netfd,&passwd_check,sizeof(int),MSG_NOSIGNAL);
            //登录成功
            printf("登录成功 real_path %s\n",real_path);
            virtual_start = strlen(real_path) - strlen(username) - 1;
        }
        else
        {
            printf("登录失败\n");
            send(netfd,&passwd_check,sizeof(int),MSG_NOSIGNAL);
        }

        while(passwd_check) 
        {
            char mkrm_path[4000] = {0};
            strcpy(mkrm_path,real_path);

            order_t order;
            //初始化order
            bzero(&order,sizeof(order_t));
            recv(netfd,&order,sizeof(order_t),MSG_WAITALL);
            printf("command = %d,opt = %s\n",order.Name,order.object);

            switch(order.Name)
            {
            case EXIT:
                printf("tid = %lu exit\n",pthread_self());
                passwd_check = 0;
                //清空通行证
                close(netfd);
                // 恢复空闲
                printf("child finish a task!\n");

                break;

            case CD:
                printf("real_path = %s\n",real_path);
                cdCmd(real_path,order.object);
                printf("cd over\n");
                bzero(&train,sizeof(train));
                for(int i = virtual_start,j = 0;i < strlen(real_path);i++,j++)
                {
                    train.data[j] = real_path[i];
                }
                train.length = strlen(train.data);
                send(netfd,&train,sizeof(int) + train.length,MSG_NOSIGNAL);
                break;

            case LS:
                printf("puts: order.num = %d, order.opt = %s\n",order.Name,order.object);
                ls(netfd,real_path);
                printf("ls over\n");
                break;

            case PUTS:
                printf("puts: order.num = %d, order.opt = %s\n",order.Name,order.object);
                putFile(order.object, netfd, real_path);
                printf("puts over\n");
                break;

            case GETS:
                printf("gets over\n");
                printf("puts: order.num = %d, order.opt = %s\n",order.Name,order.object);
                getFile(order.object, netfd, real_path);
                printf("gets over\n");
                // gets 先判断该文件大小是否>100M  大于用mmap功能
                break;

            case REMOVE:
                printf("puts: order.num = %d, order.opt = %s\n",order.Name,order.object);
                mkrm_path[strlen(mkrm_path)] = '/';
                strcat(mkrm_path,order.object);
                Remove(netfd, mkrm_path,order.object);
                printf("remove over\n");
                break;

            case PWD:
                printf("puts: order.num = %d, order.opt = %s\n",order.Name,order.object);
                pwd(netfd,real_path,virtual_start);
                printf("pwd over\n");
                break;

            case MKDIR:
                printf("puts: order.num = %d, order.opt = %s\n",order.Name,order.object);
                //strcat(buf1,real_path);
                //sprintf(real_path,"%s%s","/",buf1);
                mkrm_path[strlen(mkrm_path)] = '/';
                strcat(mkrm_path,order.object);
                Mkdir(netfd,mkrm_path,order.object);
                printf("mkdir over\n");
                break;

            case RMDIR:
                printf("puts: order.num = %d, order.opt = %s\n",order.Name,order.object);
                mkrm_path[strlen(mkrm_path)] = '/';
                strcat(mkrm_path,order.object);
                //printf("rmdir = %s\n",mkrm_path);
                Rmdir(netfd,mkrm_path,order.object);
                printf("rmdir over\n");
                break;

            default :
                break;

            }
        }

        //transfile(netfd);
        close(netfd);
        // 恢复空闲
        printf("1 worker finish a task!\n");
    }
}



int userInit(char * path)
{
    DIR *pdir = opendir(path);
    //printf("userInit path = %s\n",path);

    if(pdir == NULL)
    {
        mkdir(path,0777);
    }

    closedir(pdir);

    return 0;
}

/*
int recvn(int netfd, void* buf, int length)
{
    int total = 0;      // 已经recv的字节数
    char* p = (char*)buf;
    while(total < length)
    {
        int ret = recv(netfd, p + total, length - total, 0);
        if(ret == 0)
        {
            return -1;
        }
        total += ret;
    }

    return 0;
}*/
